from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class LoginPage():

    def __init__(self, driver):
        self.driver = driver
        # 网址
        # self.url = "http://localhost:8080/login"
        self.url = "http://wj53793py34.vicp.fun/login"
        # 登录所需控件xpath
        self.username_xpath = "//input[@placeholder='username']"
        self.password_xpath = "//input[@placeholder='password']"
        self.login_xpath = "//span[text()='登录']"
        # 校验所需控件xpath
        self.logout_div_xpath = "//div[@class='el-dropdown user-name']"
        self.logout_xpath = "(//ul[@class='el-dropdown-menu']//li)[1]"
        self.alert_xpath = "//p[text()='登录失败']"


    # 获取用户名
    @property
    def username_ele(self):
        wait = WebDriverWait(self.driver, 10)
        username = wait.until(EC.presence_of_element_located((By.XPATH, self.username_xpath)))
        return username

    # 获取密码
    @property
    def password_ele(self):
        wait = WebDriverWait(self.driver, 10)
        password = wait.until(EC.presence_of_element_located((By.XPATH, self.password_xpath)))
        return password

    # 获取登录按钮
    @property
    def button_ele(self):
        wait = WebDriverWait(self.driver, 10)
        button = wait.until(EC.visibility_of_element_located((By.XPATH, self.login_xpath)))
        return button

    # 用户名按钮
    @property
    def login_username_ele(self):
        wait = WebDriverWait(self.driver, 10)
        login_username = wait.until(EC.presence_of_element_located((By.XPATH, self.logout_div_xpath)))
        return login_username

    # 退出按钮
    @property
    def logout_button_ele(self):
        wait = WebDriverWait(self.driver, 10)
        logout_button = wait.until(EC.presence_of_element_located((By.XPATH, self.logout_xpath)))
        return logout_button

    # 提示框
    @property
    def alert_ele(self):
        wait = WebDriverWait(self.driver, 10)
        alert_msg = wait.until(EC.presence_of_element_located((By.XPATH, self.alert_xpath)))
        return alert_msg
        